﻿using Exiled.API.Features;
using RgCore.GameData;
using RgCore.GameModes;
using RgCore.PlayerHud;
using System;
using System.Collections.Generic;
using System.Linq;

namespace RgCore.Misc
{
    public static class ScoreMvpHandler
    {
        public static Dictionary<string, int> Scores { get; set; } = new Dictionary<string, int>();
        public static List<Player> KeepAlive { get; set; } = new List<Player>();

        public static event Action<Player> MvpCalled;

        public static void RegisterEvents(bool enable)
        {
            if (enable)
            {
                Exiled.Events.Handlers.Server.RestartingRound += Server_RestartingRound;
                Exiled.Events.Handlers.Server.RoundEnded += Server_RoundEnded;
                RgCore.RoundStarted += RgCore_RoundStarted;
            }
        }

        private static void Server_RoundEnded(Exiled.Events.EventArgs.Server.RoundEndedEventArgs ev)
        {
            if (!GameMode.CurrentGameMode.Attribute.ScoreEnabled || RgCore.instance.Config.ScoreForceDisabled)
            {
                return;
            }
            foreach (Player player in Player.List.Where(x => KeepAlive.Contains(x)))
            {
                player.AddScore(1500, "全局存活");
            }
            var scores = new Dictionary<string, int>(Scores);
            int score = 0;
            Player mvp = null;
            while (mvp == null && Scores.Count > 0)
            {
                var userid = scores.FirstOrDefault(x => x.Value == scores.Values.Max());
                scores.Remove(userid.Key);
                mvp = Player.Get(userid.Key);
                score = userid.Value;
            }
            if (mvp == null)
                return;
            if (score < 3000)
            {
                foreach (Player player in Player.List)
                {
                    player.SendFormattedMsg("mvp", "<color=#FFD700>MVP</color>", "无玩家分数超过3000，本局无MVP", 15, MsgType.Announcement, ScreenLocation.CenterTop, true);
                }
                return;
            }
            foreach (Player player in Player.List)
            {
                player.SendFormattedMsg("mvp", "<color=#FFD700>MVP</color>", $"<color=#00FF00>{mvp.Nickname}</color> 获得了最高分数", 15, MsgType.Announcement, ScreenLocation.CenterTop, true);
            }
            var gd = GameplayData.GameDatas.FirstOrDefault(x => x.Player == mvp);
            if (gd != null)
            {
                gd.AddMvp(1, false);
                MvpCalled?.Invoke(mvp);
            }
        }

        private static void RgCore_RoundStarted()
        {
            KeepAlive.Clear();
            foreach (Player player in Player.List)
            {
                KeepAlive.Add(player);
            }
        }

        private static void Server_RestartingRound()
        {
            Scores.Clear();
        }

        public static void AddScore(this Player player, int score, string reason)
        {
            string change = score > 0 ? $"<color=#9AFFA1>+{score}</color>" : $"<color=#FF5757>{score}</color>";
            string content = $"「 <color=#E1FF48>◉</color> | {change} {reason} 」";
            player.SendRawMsg("score", content, 3, ScreenLocation.CenterTop, true);
            GameplayData.GameDatas.FirstOrDefault(x => x.Player == player)?.AddExp((int)Math.Ceiling((double)score * 0.8), false);
            if (!GameMode.CurrentGameMode.Attribute.ScoreEnabled || RgCore.instance.Config.ScoreForceDisabled)
            {
                return;
            }
            if (!Scores.ContainsKey(player.UserId))
            {
                Scores.Add(player.UserId, 0);
            }
            Scores[player.UserId] += score;
        }

        public static void AddScoreSlight(this Player player, int score)
        {
            GameplayData.GameDatas.FirstOrDefault(x => x.Player == player)?.AddExp((int)Math.Ceiling((double)score / 10), false, true);
            if (!GameMode.CurrentGameMode.Attribute.ScoreEnabled || RgCore.instance.Config.ScoreForceDisabled)
            {
                return;
            }
            if (!Scores.ContainsKey(player.UserId))
            {
                Scores.Add(player.UserId, 0);
            }
            Scores[player.UserId] += score;
        }
    }
}